package JasPerl::JSTL::Core::ForTokensTag;

use strict;
use warnings;

use parent qw(JasPerl::JSTL::Core::LoopTagSupport);

use JasPerl::Exceptions;
use JasPerl::Util::Enumeration;

sub set_begin {
    $_[0]->{begin} = int($_[1]);
    $_[0]->validate_begin();
}

sub set_step {
    $_[0]->{step} = int($_[1]);
    $_[0]->validate_step();
}

sub set_end {
    $_[0]->{end} = int($_[1]);
    $_[0]->validate_end();
}

sub set_items {
    $_[0]->{items} = $_[1];
}

sub set_delims {
    $_[0]->{delims} = $_[1];
}

sub prepare {
    my $self = shift;
    if (not defined $self->{items}) {
        $self->{enum} = JasPerl::Util::Enumeration::EMPTY_ENUMERATION;
    } elsif (not length $self->{delims}) {
        $self->{enum} = JasPerl::Util::Enumeration->from_list($self->{items});
    } else {
        $self->{enum} = JasPerl::Util::Enumeration->from_list(
            split qr{[\Q$self->{delims}\E]}, $self->{items}
        );
    }
}

sub has_next {
    return $_[0]->{enum}->has_next();
}

sub next {
    return $_[0]->{enum}->next();
}

1;
